package com.example.demo.controller;

import com.example.demo.model.Car;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cars")
public class CarController {

    @GetMapping("/add")
    public Car addCar() {
        //https://developer.aliyun.com/article/824630
        ObjectMapper car = new ObjectMapper();
        String carJson = "{\"brand\":\"bmw\", \"doors\": 5}";
        try {
            Car carObj = car.readValue(carJson, Car.class);
            System.out.println("brand " + carObj.getBrand());
            System.out.println("doors " + carObj.getDoors());
            return carObj;
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        return null;
    }

    @GetMapping("/jsonString")
    public Car jsonString()
    {
        String carJson = "{\"brand\":\"bmw\", \"doors\": 2}";
        ObjectMapper objectMapper = new ObjectMapper();
        Reader reader = new StringReader(carJson);
        try {
            Car car = objectMapper.readValue(reader, Car.class);
            System.out.println("brand " + car.getBrand());
            System.out.println("doors " + car.getDoors());
            return car;
        }catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    @GetMapping("/multiCars")
    public List<Car> multiCars() {
        //http://localhost:8080/api/cars/multiCars
        File file = new File("src/cars.json");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<Car> carsList = objectMapper.readValue(file, new TypeReference<List<Car>>() {});
            for (Car car : carsList) {
                System.out.println("brand " + car.getBrand());
                System.out.println("doors " + car.getDoors());
                return carsList;
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        return null;

    }

    @GetMapping("/jsonToMap")
    public void JsonToMap(){
        String carJson = "{\"brand\":\"bmw\", \"doors\": 5}";

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> mapper = objectMapper.readValue(carJson, new TypeReference<Map<String, Object>>() {});
            System.out.println(mapper.getClass());
            for (Map.Entry<String, Object> entry : mapper.entrySet()) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        //class java.util.LinkedHashMap
        //brand : bmw
        //doors : 5
    }
}
